package cn.btks.resultintegration.rule.service.impl;

import cn.btks.commonserver.pojo.PageList;
import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import cn.btks.resultintegration.achievements.pojo.Achievements;
import cn.btks.resultintegration.rule.dto.rule.EwpQueryParam;
import cn.btks.resultintegration.rule.pojo.Gyqyfszlyupfqkb;
import cn.btks.resultintegration.rule.repository.GyqyfszlyupfqkbRepository;
import cn.btks.resultintegration.rule.service.GyqyfszlyupfqkbService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author lijun
 * @CompileUnit 测绘地理信息分院研发中心
 * @Date 2023/9/22 14:38
 */
@Service
public class GyqyfszlyupfqkbServiceImpl implements GyqyfszlyupfqkbService {

    @Autowired
    GyqyfszlyupfqkbRepository repository;

    @Override
    public List<Gyqyfszlyupfqkb> findAllByCity(String city) {
        List<Gyqyfszlyupfqkb> list = repository.findAllBySs(city);
        return CollectionUtils.isNotEmpty(list) ? list : null;
    }

    @Override
    public List<Gyqyfszlyupfqkb> findAll() {
        return repository.findAll();
    }

    @Override
    public List<Gyqyfszlyupfqkb> findAllByParam(EwpQueryParam param) {
        return null;
    }

    @Override
    public ResultState findPages(EwpQueryParam param) {
        PageRequest pageRequest = PageRequest.of(param.getPageNumber() - 1, param.getPageSize());
        Specification<Gyqyfszlyupfqkb> specification = new Specification<Gyqyfszlyupfqkb>() {
            @Override
            public Predicate toPredicate(Root<Gyqyfszlyupfqkb> root,
                                         CriteriaQuery<?> criteriaQuery,
                                         CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(StringUtils.isNotBlank(param.getSs())){
                    predicates.add(criteriaBuilder.equal(root.get("ss"), param.getSs()));
                }
                if(StringUtils.isNotBlank(param.getHydlmc())){
                    predicates.add(criteriaBuilder.equal(root.get("hydlmc"), param.getHydlmc()));
                }
                if(StringUtils.isNotBlank(param.getHylbmc())){
                    predicates.add(criteriaBuilder.equal(root.get("hylbmc"), param.getHylbmc()));
                }
                if(StringUtils.isNotBlank(param.getHylbmc2())){
                    predicates.add(criteriaBuilder.equal(root.get("hylbmc2"), param.getHylbmc2()));
                }
                Predicate[] predicates1 = new Predicate[predicates.size()];
                return criteriaBuilder.and(predicates.toArray(predicates1));
            }
        };
        Page<Gyqyfszlyupfqkb> configPage = repository.findAll(specification, pageRequest);
        if (configPage.getTotalElements() > 0) {
            PageList pageList = new PageList();
            pageList.setResult(configPage.getContent());
            pageList.setPageNum(configPage.getTotalPages());
            pageList.setTotalCount(configPage.getTotalElements());
            return new ResultState(StateInfo.FIND_OK.getCode(), StateInfo.FIND_OK.getMsg(), pageList);
        }
        return new ResultState(StateInfo.FIND_NULL.getCode(), StateInfo.FIND_NULL.getMsg());
    }
}
